<?php

class Controller_Test_Redis extends Controller_Test_Abstract
{
    private $_redis;
    protected $_urlLink = '/_test/redis/';

    public function init()
    {
        $this->_redis = new Com_Cache_Redis('blog');
    }

    public function indexAction()
    {
        $this->_dump($this->_redis);
        parent::indexAction();
    }

    public function stringAction()
    {
        $this->_redis->set('testkey', time());
        $this->_dump($this->_redis->get('testkey'));
    }

    public function listAction()
    {
        // push
        for ($i = 90; $i < 112; $i++) {
            $value = json_encode(array(
                'uid' => $i,
                'username' => 'test',
                'dateline' => uniqid(),
            ));
            vd($this->_redis->rpush('Queue:TEST_PUSH', $value), 0);
        }

        // pop
        vd($this->_redis->lpop('Queue:TEST_PUSH'), 0);

        // view
        vd($this->_redis->lrange('Queue:TEST_PUSH', 0, -1), 0);
    }

    public function hashAction()
    {
        // hset
        $this->_redis->delete('testhash');
        $this->_redis->hset('testhash', 'key-1', '100');

        $setArr = array();
        for ($i = 2; $i <= 20; $i++) {
            $setArr['key-' . $i] = 'value-' . $i;
        }
        $f = $this->_redis->hmset('testhash', $setArr);


        vd($this->_redis->hmget('testhash', array('key-1', 'key-2')), 0);
        vd($this->_redis->hgetAll('testhash'), 0);
        vd($this->_redis->hkeys('testhash'), 0);
        vd($this->_redis->hvals('testhash'), 0);
        vd($this->_redis->hlen('testhash'), 0);

        // hincr
        vd($this->_redis->hincrby('testhash', 'key-1', 10), 0);
        vd($this->_redis->hget('testhash', 'key-1'), 0);
    }

    public function setAction()
    {

    }

    public function zsetAction()
    {
        // zadd
        $this->_redis->delete('zset_test');
        for ($fuid = 0; $fuid <= 50; $fuid+=10) {
            $this->_redis->zadd('zset_test', $fuid, $fuid);
        }

        $this->_dump($this->_redis->zrange('zset_test', 0, -1)) . '<br />';
        $this->_dump($this->_redis->zrank('zset_test', 50)) . '<br />';
        $this->_dump($this->_redis->zincrby('zset_test', 5, 50)) . '<br />';
        $this->_dump($this->_redis->zrange('zset_test', 0, -1, 'WITHSCORES')) . '<br />';
    }


    public function infoAction()
    {
        vd($this->_redis->info());
    }

    public function flushAction()
    {
        vd($this->_redis->flushall());
    }

    public function arrayAction()
    {
        $setArr = array();
        for ($i = 1; $i < 30; $i++) {
            $setArr['test-' . $i] = 'value-\'s' . $i;
        }

        vd($this->_redis->set('testhash-json', $setArr), 0);
        vd($this->_redis->get('testhash-json'), 0);

        // 数组务必序列化后存进去
        vd($this->_redis->set('testhash-json', json_encode($setArr)), 0);
        vd(json_decode($this->_redis->get('testhash-json'), true), 0);
    }
}